package com.fast.manager.base.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.fast.commons.SystemHelper;
import com.fast.commons.constants.BaseConst;
import com.fast.commons.utils.CookieKit;
import com.fast.commons.web.BaseController;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.base.domain.Role;
import com.fast.manager.base.domain.User;
import com.fast.manager.base.service.UserService;

@Controller
@RequestMapping("/manager/base/user")
public class UserController extends BaseController {
	
	@Autowired
	private UserService service ;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add(User dto) {
		return this.service.add(dto);
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@RequestMapping(value="/deleteBatch", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse deleteBatch(Long[] ids) {
		return this.service.deleteBatch(ids);
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update(User dto) {
		return this.service.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody User get(@PathVariable Long id) {
		return this.service.get(id) ;
	}
	
	/**
	 * 分页列表
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/datagrid", method=RequestMethod.GET)
	public @ResponseBody DataGrid<User> datagrid(User dto) {
		return this.service.datagrid(dto) ;
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse login(String account, String password, boolean remember) {
		AjaxResponse ajaxResponse = this.service.login(account, password) ;
		if(remember) {
			saveCookie(account, password);
		} else {
			CookieKit.clearCookies(request, response, BaseConst.COOKIE_NAME_LOGIN_KEY);
		}
		return ajaxResponse ;
	}
	
	//保存Cookie
	private void saveCookie(String account, String password) {
		String cookie = account+":"+password+":"+System.currentTimeMillis();
		//默认保存用户账号的Cookie
		Map<String, String> cookieParams = new HashMap<String, String>() ;
		cookieParams.put(BaseConst.COOKIE_NAME_LOGIN_KEY, Base64.encodeBase64URLSafeString(cookie.getBytes())); //Base64进行编码
		CookieKit.addCookies(response, cookieParams, 60 * 60 * 24 * 30);
	}
	
	/**
	 * 退出系统
	 */
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logout() {
		SystemHelper.logout();
		return InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/manager/login";
	}
	
	/**
	 * 找回密码
	 * @param email
	 * @param validateCode
	 * @return
	 */
	@RequestMapping(value="/iforgotPassword", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse iforgotPassword(String email, String validateCode, HttpSession session) {
		String kaptcha = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY) ;
		if(null == kaptcha) {
			return new AjaxResponse().fail("验证码过期，请刷新！") ;
		}
		if(kaptcha.equals(validateCode)) {
			return this.service.iforgotPassword(email) ;
		} else {
			return new AjaxResponse().fail("验证码不正确！") ;
		}
		
	}
	
	/**
	 * 检查找回密码的链接地址是否正确
	 * @param newPassword
	 * @return
	 */
	@RequestMapping(value="/iforgotCheckLink/{digitalSignature}", method=RequestMethod.GET)
	public String iforgotCheckLink(@PathVariable String digitalSignature, Model model) {
		model.addAttribute("resp", this.service.iforgotCheckLink(digitalSignature)) ;
		return "/manager/iforgot" ;
	}
	
	/**
	 * 密码找回的重设密码
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/iforgotByResetPassword", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse iforgotByResetPassword(Long id, String newPassword) {
		AjaxResponse ajaxResponse = this.service.iforgotByResetPassword(id, newPassword);
		CookieKit.clearCookies(request, response, BaseConst.COOKIE_NAME_LOGIN_KEY);
		return ajaxResponse ;
	}
	
	/**
	 * 添加角色到用户窗口
	 * @return
	 */
	@RequestMapping(value="/addRoleToUser_dialog/{companyId}", method=RequestMethod.GET)
	public String addRoleToUser_dialog(@PathVariable String companyId, Model model) {
		if(StringUtils.isAllLowerCase(companyId)) {
			model.addAttribute("companyId", companyId);
		}
		return viewName("addRoleToUser_dialog") ;
	}
	
	/**
	 * 添加角色到用户
	 * @param user_ids
	 * @param role_ids
	 * @return
	 */
	@RequestMapping(value="/addRoleToUser", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse addRoleToUser(String user_ids, String role_ids) {
		return this.service.addRoleToUserBatch(user_ids, role_ids) ;
	}
	
	/**
	 * 批量重置用户密码窗口
	 * @return
	 */
	@RequestMapping(value="/reset_pwd_dialog", method=RequestMethod.GET)
	public String resetPwdBatch_dialog() {
		return viewName("reset_pwd_dialog") ;
	}
	
	/**
	 * 批量重置用户密码
	 * @param password
	 * @param user_ids
	 * @return
	 */
	@RequestMapping(value="/resetPwdBatch", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse resetPwdBatch(String password, String user_ids) {
		return this.service.resetPwdBatch(password, user_ids) ;
	}
	
	/**
	 * 批量调换部门窗口
	 * @return
	 */
	@RequestMapping(value="/change_dept_dialog", method=RequestMethod.GET)
	public String change_dept_dialog() {
		return viewName("change_dept_dialog") ;
	}
	
	/**
	 * 批量调换部门
	 * @param password
	 * @param user_ids
	 * @return
	 */
	@RequestMapping(value="/changeDeptBatch", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse changeDeptBatch(Long company_id, Long dept_id, String user_ids) {
		return this.service.changeDeptBatch(company_id, dept_id, user_ids);
	}
	
	
	@RequestMapping(value="/getUserRoles/{user_id}", method=RequestMethod.GET)
	public @ResponseBody List<Role> getUserRoles(@PathVariable Long user_id) {
		return this.service.getUserRoles(user_id);
	}
	
}
